package top.tagao.yygh.hosp.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;
import top.tagao.yygh.common.exception.YyghException;
import top.tagao.yygh.common.helper.HttpRequestHelper;
import top.tagao.yygh.common.result.Result;
import top.tagao.yygh.common.result.ResultCodeEnum;
import top.tagao.yygh.common.utils.MD5;
import top.tagao.yygh.hosp.service.DepartmentService;
import top.tagao.yygh.hosp.service.HospitalService;
import top.tagao.yygh.hosp.service.HospitalSetService;
import top.tagao.yygh.hosp.service.ScheduleService;
import top.tagao.yygh.model.hosp.Department;
import top.tagao.yygh.model.hosp.Hospital;
import top.tagao.yygh.model.hosp.Schedule;
import top.tagao.yygh.vo.hosp.DepartmentQueryVo;
import top.tagao.yygh.vo.hosp.ScheduleQueryVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/3 15:46
 */

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //前面校验的接口
    private boolean SignCheck(Map<String, Object> paramMap){
        //1获取医院(数据库) manage 系统传递过来的签名 ;签名进行MD5加密
        String manageSign = (String) paramMap.get("sign");

        //2 根据医院(数据库) shop 传递过来医院编号，查询数据库(shop) ，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String shopSign = hospitalSetService.getSignKey(hoscode);

        //3 把查询的数据进行MD5的进行加密
        String md5ShopSign = MD5.encrypt(shopSign);
        //4 判断签名是否一致
        if(!manageSign.equals(md5ShopSign)){
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return true;
    }



    //删除排班接口
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");


        scheduleService.remove(hoscode,hosScheduleId);

        return Result.ok();
    }



    //查询排班接口
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){

        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        //签名校验
        SignCheck(paramMap);

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班接口
    @PostMapping("saveSchedule")
    public Result  saveSchedule(HttpServletRequest request){
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //签名校验
        SignCheck(paramMap);
        scheduleService.save(paramMap);
        return Result.ok();
    }






    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号 和 科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        //签名校验
        SignCheck(paramMap);

        return  departmentService.remove(hoscode,depcode) ? Result.ok() : Result.fail();
    }


    //查询科室接口
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来科室信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        //签名校验
        SignCheck(paramMap);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(pageModel);
    }


    //上传科室接口
    @PostMapping("saveDepartment")
    public  Result saveDepartment(HttpServletRequest request){
        //获取前台传过来的Map<String, String[]> 数据, 转换为  Map<String, Object>
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名校验
        SignCheck(paramMap);
        //调用service的方法
        departmentService.save(paramMap);

        return  Result.ok();
    }






    //上传医院接口 {
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取前台传过来的Map<String, String[]> 数据, 转换为  Map<String, Object>
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //签名校验
        SignCheck(paramMap);

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData =(String)paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData",logoData);

        //调用service方法
        hospitalService.save(paramMap);
        return  Result.ok();
    }
    //查询医院
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){

        //获取医院传递来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);


        //调用service方法实现根据医院编号查询
        String hoscode = (String)paramMap.get("hoscode");
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }


}
